package ru.mail.getpwd;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;

public class Login {
	private static final String ENCODING = "Windows-1251";

	private String login;

	private String host;

	private StringBuilder postDataBuffer;

	private InetAddress authHost;

	public Login(String login, String host) {
		this.login = login;
		this.host = host;
	}

	public boolean login(String password) throws LoginException {
		// post data
		if (postDataBuffer == null) {
			try {
				preparePostData();
			} catch (UnsupportedEncodingException e) {
				throw new LoginException("Unsupported Encoding: " + ENCODING, e);
			}
		}

		String postData = postDataBuffer.toString() + password;

		if (authHost == null) {
			try {
				authHost = InetAddress.getByName("win.mail.ru");
			} catch (UnknownHostException e) {
				throw new LoginException("Unknown Host: win.mail.ru", e);
			}
		}

		// Create a socket to the host
		Socket socket;
		try {
			socket = new Socket(authHost, 80);
		} catch (IOException e) {
			throw new LoginException("Can't create connection.", e);
		}

		// Send header
		try {
			BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream(), ENCODING));

			wr.write("POST /cgi-bin/auth HTTP/1.0\r\n");
			wr.write("Content-Length: " + postData.length() + "\r\n");
			wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
			wr.write("\r\n");
			wr.write(postData);
			wr.flush();
		} catch (UnsupportedEncodingException e) {
			throw new LoginException("Unsupported Encoding: " + ENCODING, e);
		} catch (IOException e) {
			throw new LoginException(
					"Error occurs while sending POST request: "
							+ e.getMessage(), e);
		}

		// check response
		String line;
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			line = rd.readLine();
			if (line == null) {
				throw new LoginException("Response is null");
			}
		} catch (IOException e) {
			throw new LoginException(
					"Error occurs while receiving data: "
							+ e.getMessage(), e);
		}
		
		return line.indexOf(" 302 ") > -1;
	}

	private void preparePostData() throws UnsupportedEncodingException {
		postDataBuffer = new StringBuilder();

		postDataBuffer.append(URLEncoder.encode("page", ENCODING)).append("=");
		postDataBuffer.append(URLEncoder.encode("", ENCODING));

		postDataBuffer.append("&");
		postDataBuffer.append(URLEncoder.encode("post", ENCODING)).append("=");
		postDataBuffer.append(URLEncoder.encode("", ENCODING));

		postDataBuffer.append("&");
		postDataBuffer.append(URLEncoder.encode("login_from", ENCODING))
				.append("=");
		postDataBuffer.append(URLEncoder.encode("", ENCODING));

		postDataBuffer.append("&");
		postDataBuffer.append(URLEncoder.encode("Login", ENCODING)).append("=");
		postDataBuffer.append(URLEncoder.encode(this.login, ENCODING));

		postDataBuffer.append("&");
		postDataBuffer.append(URLEncoder.encode("Domain", ENCODING))
				.append("=");
		postDataBuffer.append(URLEncoder.encode(this.host, ENCODING));

		postDataBuffer.append("&");
		postDataBuffer.append(URLEncoder.encode("Password", ENCODING)).append(
				"=");

	}

	public static void main(String[] args) throws LoginException {
		Login l = new Login("a.martynov", "list.ru");
		if (l.login("xxx")) {
			System.out.println("ok");
		} else {
			System.out.println("incorrect");
		}
	}
}
